package com.zerox.controller;


import com.alibaba.fastjson.JSON;
import com.zerox.domain.TableVo;
import com.zerox.pojo.sysRole;
import com.zerox.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/list")
    @ResponseBody
    public void sysRole(Model model, HttpServletResponse response, Integer page, Integer limit) throws IOException {

        List<sysRole> sysRoleList = sysRoleService.list((page - 1) * limit, limit);
        TableVo tableVo = new TableVo(0, 2, sysRoleList, "");
        String json = JSON.toJSONString(tableVo);
        response.getWriter().print(json);
    }

    @GetMapping("/toSysRole")
    public String toSysRole() {
        return "role/sysRoleList";
    }

    @GetMapping("/view/{role_id}")
    public String view(Model model, @PathVariable Integer roleId){
        sysRole sysRole = sysRoleService.selectByrole_id(roleId);
        model.addAttribute("sysRole",sysRole);
        return "view";
    }

    @GetMapping("/update/{roleId}")
    public String Update(Model model, HttpSession session,@PathVariable Integer roleId){
        sysRole sysRole = sysRoleService.selectByrole_id(roleId);
        model.addAttribute("sysRole",sysRole);
        return "role/roleUpdate";
    }


    @PostMapping("/update")
    @ResponseBody
    public String update(Model model,sysRole sysRole){
        int result = sysRoleService.update(sysRole);
        if (result>0){
            return "true";
        }
        return "false";
    }

    @GetMapping("/roleDelete/{roleId}")
    @ResponseBody
    public String delete(@PathVariable Integer roleId){
        int result = sysRoleService.delete(roleId);
        return result > 0 ? "true" : "false";
    }

    @GetMapping("toAdd")
    public String add(){
        return "role/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public String Add(Model model,@Valid sysRole sysRole){
        int result = sysRoleService.add(sysRole);
        if (result>0){
            return "true";
        }
        return "false";
    }
}
